package com.chinasofti.project.components;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 记录日志的拦截器,配置请参考ssm-servlet.xml
 * @author Administrator
 *
 */
@Component
public class ParamLoggingInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(ParamLoggingInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
	        String requestURI = request.getRequestURI();
	        Map<String, String[]> parameterMap = request.getParameterMap();
	        StringBuilder paramLogBuilder = new StringBuilder();
	        paramLogBuilder.append("请求[" + requestURI + "]");
	        if (parameterMap.size() > 0) {
	            paramLogBuilder.append(",参数为");
	            for (Map.Entry<String, String[]> e : parameterMap.entrySet()) {
	                paramLogBuilder.append(e.getKey() + "=");
	                if (e.getValue().length > 1) {
	                    paramLogBuilder.append(Arrays.toString(e.getValue()) + ";");
	                } else {
	                    paramLogBuilder.append(e.getValue()[0] + ";");
	                }
	            }
	        }
	        logger.info(paramLogBuilder.toString());
		return true;
	}
}
